package org.example.websocket.services;

import com.alibaba.fastjson2.JSONObject;
import org.example.websocket.constants.ComConstants;
import org.example.websocket.domain.MsgVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/****
 * @author songshiming
 * @date 2023/1/9
 * @desc  接受redis 订阅的频道
 */
public class RedisSubscribe implements MessageListener {
    // 日志对象
    private static final Logger log = LoggerFactory.getLogger(RedisSubscribe.class);

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String channel = new String(message.getChannel());
        log.info("订阅频道:" + channel);

        String body = new String(message.getBody());
        log.info("接收数据:" +body );

        if(ComConstants.TOPIC_CHANNEL_SEND.equals(channel)){
            try {
                MsgVo msgVo = JSONObject.parseObject(body, MsgVo.class);

                WebSocketServer.sendInfo(msgVo.getMessage(),msgVo.getSid());
            }catch (Exception e ){
                e.printStackTrace();
            }
        }
    }
}

